//
//  AccountView.m
//  PigMarket
//
//  Created by Felix on 2019/3/6.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AccountView.h"
#import "AccountDropCell.h"


#define Spacing 30

#define mAnimationTime 0.5f
#define mCellHeight 48.f

@interface AccountView ()<UITableViewDelegate, UITableViewDataSource>

/**
 账号
 */
@property (nonatomic , strong) UILabel *accountLabel;


/**
 验证码/密码
 */
@property (nonatomic , strong) UILabel *verifyLabel;


/**
 下划线
 */
@property (nonatomic , strong) UILabel *verifyBottomLine;


/**
 tableView
 */
@property (nonatomic, strong) UITableView *tableView;


/**
 偏移量
 */
@property (nonatomic, assign) CGFloat offsetY;


/**
 当前选中Index
 */
@property (nonatomic, assign) NSInteger currentIndex;


@end

@implementation AccountView

#pragma mark - init method
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initView];
        [self addDropMenuView];
        [self initLayout];
    }
    return self;
}

/**
 添加展示层
 */
- (void)addDropMenuView{
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window addSubview:self.tableView];
}


- (void)initView {
    // 添加子控件
    [self addSubview:self.accountLabel];
    [self addSubview:self.accountField];
    [self addSubview:self.pulldownBtn];
    [self addSubview:self.accountBottomLine];
    
    [self addSubview:self.verifyLabel];
    [self addSubview:self.verifyField];
    [self addSubview:self.verifyBtn];
    [self addSubview:self.hiddenBtn];

    [self addSubview:self.verifyBottomLine];
}


- (void)initLayout {
    // 布局子控件
    [self.accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(Spacing);
    }];
    
    [self.accountLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.pulldownBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-Spacing);
        make.centerY.mas_equalTo(self.accountLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    [self.accountField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.accountLabel.mas_right).offset(Spacing);
        make.centerY.mas_equalTo(self.accountLabel.mas_centerY);
        make.right.mas_equalTo(self.pulldownBtn.mas_left).offset(-8);
    }];
    
    [self.accountBottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Spacing);
        make.top.mas_equalTo(self.accountLabel.mas_bottom).offset(Spacing/2);
        make.right.mas_equalTo(-Spacing);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.verifyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Spacing);
        make.top.mas_equalTo(self.accountBottomLine.mas_bottom).offset(Spacing);
    }];
    [self.verifyLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

    [self.verifyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-Spacing);
        make.centerY.mas_equalTo(self.verifyLabel.mas_centerY);
        make.height.mas_equalTo(36);
    }];
    [self.hiddenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-Spacing);
        make.centerY.mas_equalTo(self.verifyLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];

    [self.verifyField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.verifyLabel.mas_right).offset(Spacing);
        make.centerY.mas_equalTo(self.verifyLabel.mas_centerY);
        make.right.mas_equalTo(self.verifyBtn.mas_left).offset(-8);
    }];
    [self.verifyBottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Spacing);
        make.top.mas_equalTo(self.verifyLabel.mas_bottom).offset(Spacing/2);
        make.right.mas_equalTo(-Spacing);
        make.height.mas_equalTo(0.5);
        make.bottom.mas_equalTo(0);
    }];
}


- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    //展开TableView的偏移量
    self.offsetY = self.mj_y + self.height/2.0;
    self.tableView.mj_y = self.offsetY;
}


#pragma mark - target action
- (void)hiddenBtnTapped {
    self.hiddenBtn.selected = !self.hiddenBtn.selected;
    self.verifyField.secureTextEntry = !self.verifyField.secureTextEntry;
}


#pragma mark - show/hide/remove box methods

- (void)showView{
    
    [self.tableView reloadData];
    NSInteger rows = self.phones.count;
    
    @weakify(self);
    [UIView animateWithDuration:mAnimationTime animations:^{
        @strongify(self);
        self.tableView.frame = CGRectMake(0, self.offsetY, KScreenWidth, rows *mCellHeight +2);
        
    }];
}


- (void)dissView{
    
    @weakify(self);
    [UIView animateWithDuration:mAnimationTime animations:^{
        @strongify(self);
        
        if (self.tableView.height >0) {
            self.tableView.frame = CGRectMake(0, self.offsetY, KScreenWidth, 0);
        }
    }];
}


- (void)removeScreenView{
    
    if (self.tableView) {
        self.tableView.hidden = YES;
        [self.tableView removeFromSuperview];
    }
}


- (void)test:(UIButton *)sender{
    
    if (sender.selected) {
        self.pulldownBtn.selected = NO;
        [self dissView];
    }else{
        self.pulldownBtn.selected = YES;
        [self showView];
    }
}


#pragma mark - tableview.datasource/delegate methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.phones.count ;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AccountDropCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccountDropCell"];
    cell.account.text = self.phones[indexPath.row];
    
    //删除Block
    @weakify(self);
    cell.dealDeletePhotoBlock = ^{
        @strongify(self);
        
        NSMutableArray *mutableArrayCopy = [[kUserDefaults objectForKey:kUserAccountListKey] mutableCopy];
        if (mutableArrayCopy && mutableArrayCopy.count > indexPath.row ) {
            
            [mutableArrayCopy removeObjectAtIndex:indexPath.row];
            [kUserDefaults setObject:mutableArrayCopy forKey:kUserAccountListKey];
            [kUserDefaults synchronize];
        }
        
        self.phones = [[kUserDefaults objectForKey:kUserAccountListKey] mutableCopy];
        [self.tableView reloadData];
    };
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *aims = self.phones[indexPath.row];
    self.accountField.text = aims;
    [self dissView];
}


#pragma mark - setter methods

- (void)setSelectMessageLogin:(BOOL)selectMessageLogin {
    _selectMessageLogin = selectMessageLogin;
    if (selectMessageLogin) {
        self.verifyLabel.text = @"验证码";
        self.verifyField.placeholder = @"请输入验证码";
        self.verifyField.secureTextEntry = NO;
        self.verifyField.text = nil;
        self.verifyBtn.hidden = NO;
        self.hiddenBtn.hidden = YES;
        self.verifyField.keyboardType = UIKeyboardTypeNumberPad;
    }
}


- (void)setSelectPasswordLogin:(BOOL)selectPasswordLogin {
    _selectPasswordLogin = selectPasswordLogin;
    if (selectPasswordLogin) {
        self.verifyLabel.text = @"密　码";
        self.verifyField.placeholder = @"请输入密码";
        self.verifyField.secureTextEntry = YES;
        self.verifyField.text = nil;
        self.verifyBtn.hidden = YES;
        self.hiddenBtn.hidden = NO;
        self.verifyField.keyboardType = UIKeyboardTypeDefault;
    }
}

#pragma mark - getter methods

- (UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 0) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorColor = KClearColor;
        _tableView.backgroundColor = KWhiteColor;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.rowHeight = mCellHeight;
        _tableView.bounces = NO;
        [_tableView registerClass:[AccountDropCell class] forCellReuseIdentifier:@"AccountDropCell"];
        
        UIView *view = [UIView new];
        view.backgroundColor = UIColorFromRGB(0xDFDFDF);
        view.frame = CGRectMake(0, 0, KScreenWidth , 1);
        
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(40, 0, KScreenWidth - 80, 1)];
        maskLayer.path = path.CGPath;
        view.layer.mask = maskLayer;
        _tableView.tableFooterView = view;
    }
    return _tableView;
}


- (UILabel *)accountLabel {
    if (!_accountLabel) {
        _accountLabel = [[UILabel alloc] init];
        _accountLabel.textColor = [UIColor textBlackColor];
        _accountLabel.font = [UIFont largerFont];
        _accountLabel.text = @"账　号";
    }
    return _accountLabel;
}


- (UITextField *)accountField {
    if (!_accountField) {
        _accountField = [[UITextField alloc] init];
        _accountField.placeholder = @"请输入手机号";
        _accountField.textColor = [UIColor textBlackColor];
        _accountField.font = [UIFont largerFont];
        _accountField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        _accountField.keyboardType = UIKeyboardTypeNumberPad;
        _accountField.returnKeyType = UIReturnKeyNext;
    }
    return _accountField;
}


- (UIButton *)pulldownBtn {
    if (!_pulldownBtn) {
        _pulldownBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_pulldownBtn setImage:[UIImage imageNamed:@"login_pulldown"] forState:UIControlStateNormal];
        [_pulldownBtn setImage:[UIImage imageNamed:@"login_pulldown_selected"] forState:UIControlStateSelected];
        _pulldownBtn.adjustsImageWhenHighlighted = NO;
        _pulldownBtn.selected = NO;
        [_pulldownBtn addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _pulldownBtn;
}


- (UILabel *)accountBottomLine {
    if (!_accountBottomLine) {
        _accountBottomLine = [[UILabel alloc] init];
        _accountBottomLine.backgroundColor = [UIColor dividerColor];
    }
    return _accountBottomLine;
}


- (UILabel *)verifyLabel {
    if (!_verifyLabel) {
        _verifyLabel = [[UILabel alloc] init];
        _verifyLabel.textColor = [UIColor textBlackColor];
        _verifyLabel.font = [UIFont largerFont];
        _verifyLabel.text = @"验证码";
    }
    return _verifyLabel;
}


- (UITextField *)verifyField {
    if (!_verifyField) {
        _verifyField = [[UITextField alloc] init];
        _verifyField.placeholder = @"请输入验证码";
        _verifyField.textColor = [UIColor textBlackColor];
        _verifyField.font = [UIFont largerFont];
        _verifyField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        _verifyField.returnKeyType = UIReturnKeyNext;
    }
    return _verifyField;
}


- (VerifyCodeButton *)verifyBtn {
    if (!_verifyBtn) {
        _verifyBtn = [VerifyCodeButton buttonWithType:UIButtonTypeCustom];
        _verifyBtn.hidden = NO;
    }
    return _verifyBtn;
}


- (UIButton *)hiddenBtn {
    if (!_hiddenBtn) {
        _hiddenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_hiddenBtn setImage:[UIImage imageNamed:@"login_hidden"] forState:UIControlStateNormal];
        [_hiddenBtn setImage:[UIImage imageNamed:@"login_show"] forState:UIControlStateSelected];
        _hiddenBtn.selected = NO;
        _hiddenBtn.adjustsImageWhenHighlighted = NO;
        _hiddenBtn.hidden = YES;
        [_hiddenBtn addTarget:self action:@selector(hiddenBtnTapped) forControlEvents:UIControlEventTouchUpInside];
    }
    return _hiddenBtn;
}


- (UILabel *)verifyBottomLine {
    if (!_verifyBottomLine) {
        _verifyBottomLine = [[UILabel alloc] init];
    }
    return _verifyBottomLine;
}


@end
